package com.damei.qmui_test_demo.httpService;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.Callback;

import java.util.HashMap;
import java.util.Map;

public class HttpService {
    private volatile static HttpService instance;

    public static HttpService getInstance() {
        if (instance == null) {
            synchronized (HttpService.class) {
                if (instance == null) {
                    instance = new HttpService();
                }
            }
        }
        return instance;
    }

    /**
     * POST请求(form提交)
     * @param url
     * @param param
     * @param handler
     */
    public void okgoFormPostRequest(String url, Map<String, String> param, Object handler) {
        if (param == null) {
            param = new HashMap<String, String>();
        }
        param.put("userId", "");
        OkGo.<String>post(HttpUrlConstants.HTTP_URL + url)
                .tag(0)
                .params(param)
                .headers("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
                .headers("token", "")
                .execute((Callback)handler);
    }

    /**
     * POST请求(Json提交)
     * @param url
     * @param param
     * @param handler
     */
    public void okgoJsonPostRequest(String url, Map<String, String> param, HttpStringResponseHandler<Object> handler) {
        if (param == null) {
            param = new HashMap<String, String>();
        }
        param.put("userId", "");
        OkGo.<String>post(HttpUrlConstants.HTTP_URL + url)
                .tag(0)
                .params(param)
                .headers("Content-Type", "application/json; charset=utf-8")
                .headers("token", "")
                .execute(handler);
    }
}
